package algorithm.unionfind;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class UnionFindTestHelper {

    public static<T extends UnionFind> void testUF(Class<T> ufClz,int n) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Constructor<T> constructor = ufClz.getConstructor(int.class);
        UnionFind unionFind = constructor.newInstance(n);

        long startTime = System.currentTimeMillis();

        // 进行n次操作, 每次随机选择两个元素进行合并操作
        for( int i = 0 ; i < n ; i ++ ){
            int a = (int)(Math.random()*n);
            int b = (int)(Math.random()*n);
            unionFind.union(a,b);
        }
        // 再进行n次操作, 每次随机选择两个元素, 查询他们是否同属一个集合
        for(int i = 0 ; i < n ; i ++ ){
            int a = (int)(Math.random()*n);
            int b = (int)(Math.random()*n);
            unionFind.isConnected(a,b);
        }
        long endTime = System.currentTimeMillis();

        // 打印输出对这2n个操作的耗时
        System.out.println(ufClz.getSimpleName()+", " + 2*n + " ops, " + (endTime-startTime) + "ms");
    }


    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        int n = 20000;
        UnionFindTestHelper.testUF(UnionFind1.class,n);
        UnionFindTestHelper.testUF(UnionFind2.class,n);
        UnionFindTestHelper.testUF(UnionFindSize.class,n);
    }
}
